//////////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2014-2015, Egret Technology Inc.
//  All rights reserved.
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the Egret nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
//  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//  IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
//  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
//  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
//  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////////////////

class LoadingUI extends egret.Sprite {
    private pgBg: egret.Bitmap;
    private pgBar: egret.Bitmap;
    private textField: egret.TextField;
    private bg: egret.Bitmap;
    private decTxt: egret.TextField;

    private w: number = 0;
    private h: number = 0;
    private static _me: LoadingUI;
    public static get me() {
        if (this._me == null)
            this._me = new LoadingUI();
        return this._me;
    }
    public constructor() {
        super();
        SoundManager.me;
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemove, this);

        this.createView();
    }
    public fuBmp: egret.Bitmap = new egret.Bitmap();

    private loadbar: egret.Bitmap = new egret.Bitmap();
    private onRemove(event: egret.Event) {

    }
    private onAddToStage(event: egret.Event) {
        this.removeEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);






        // RES.getResByUrl("resource/assets/preload/start_bg.jpg", function (texture: egret.Texture): void {
        //     this.fuBmp.texture = texture;
        //     this.addChildAt(this.fuBmp, 0);
        //     this.fuBmp.width = this.stage.stageWidth;
        //     this.fuBmp.height = this.stage.stageHeight;

        // }, this, RES.ResourceItem.TYPE_IMAGE);
        // RES.getResByUrl("resource/assets/preload/loading_baa.png", function (texture: egret.Texture): void {
        //     this.loadbar.texture = texture;
        //     this.addChildAt(this.loadbar, this.getChildIndex(this.textField));
        //     this.loadbar.width = 800;
        //     this.loadbar.height = 50;
        //     this.loadbar.x = (this.stage.stageWidth - this.loadbar.width) / 2;
        //     this.loadbar.y = this.textField.y - 12;
        // }, this, RES.ResourceItem.TYPE_IMAGE);
    }




    private createView(): void {
        this.w = egret.MainContext.instance.stage.stageWidth;
        this.h = egret.MainContext.instance.stage.stageHeight;

        this.bg = new egret.Bitmap;
        this.bg.texture = RES.getRes("PreLoadingBg_jpg");
        this.addChild(this.bg);

        this.pgBg = new egret.Bitmap;
        this.pgBg.texture = RES.getRes("PreLoadingBarBg_png");
        this.pgBg.x = this.w / 2 - this.pgBg.width / 2;
        this.pgBg.y = this.h - this.pgBg.height - 150;
        this.addChild(this.pgBg);

        this.pgBar = new egret.Bitmap;
        this.pgBar.texture = RES.getRes("PreLoadingBar_png");
        this.pgBar.x = this.w / 2 - this.pgBar.width / 2;
        this.pgBar.y = this.pgBg.y + 20;
        this.addChild(this.pgBar);

        this.textField = new egret.TextField();
        this.textField.size = 24;
        this.textField.textColor = 0xFFFFFF;
        this.textField.bold = true;
        this.textField.stroke = 1;
        this.textField.strokeColor = 0x000000;
        this.addChild(this.textField);
        this.textField.width = 280;
        this.textField.x = this.w / 2 - this.textField.width / 2;
        this.textField.y = this.pgBg.y - 20;
        this.textField.textAlign = "center";
        this.textField.text = "0%";

        this.pgBar.width = 0;

        // this.decTxt = new egret.TextField();
        // //this.decTxt.text = "建议在wifi或4G情况下进入游戏,体验更加流畅\n\n手机锁屏，体验更好";
        // this.addChild(this.decTxt);
        // this.decTxt.textColor = 0xfff003;
        // this.decTxt.width = 700;
        // this.decTxt.height = 50;
        // this.decTxt.textAlign = "center";
        // this.decTxt.x = (this.stage.stageWidth - this.decTxt.width) / 2;
        // this.decTxt.y = 585;
    }

    public setProgress(current1: number, total1: number, current2: number, total2: number): void {
        if (this.textField)
            this.textField.text = "Loading..." + current1 + "/" + total1 + "(" + current2 + "/" + total2 + ")";

        var rate: number = Math.round((current2 / total2) * 100);
        this.textField.text = rate + "%";
        let w = this.pgBg.width * (current2 / total2);
        if(w>=this.pgBg.width){
            w = this.pgBg.width-57;
        }
        this.pgBar.width = w;
    }

}
